﻿using System;
using System.Xml;
using System.Drawing;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DeveloperUtility.Security;

using NetExp.MediaXenter.Database;

namespace NetExp.MediaXenter.Utilities
{
    public class StyleHelper
    {
        private string themeName;

        private FontStyle fontNormale;
        private FontStyle fontTitoloSottoMenu1;

        public FontStyle FontNormale
        {
            get
            {
                return fontNormale;
            }
        }

        public FontStyle FontTitoloSottomenu1
        {
            get
            {
                return fontTitoloSottoMenu1;
            }
        }
       
        public StyleHelper(string theme)
        {
            themeName = theme;
            fontNormale = SetFont("Normale");
            fontTitoloSottoMenu1 = SetFont("TitoloSottomenu1");
        }


        internal static XmlDocument GetStylesDocument(string theme, string path)
        {
            XmlDocument documento = new XmlDocument();
            documento.Load(path + "\\" + theme + "\\Styles.xml");
            return documento;
        }

        private FontStyle SetFont(string titoloFont)
        {
            XmlDocument documento = GetStylesDocument(themeName, Constants.STYLEPATH);

            string fontName = string.Empty;
            float fontSize= 0;
            bool fontBold=false;
            bool fontItalic = false;
            bool fontUnderline = false;
            string colore = string.Empty;

            IEnumerator numerator = documento.ChildNodes.GetEnumerator();

            while (numerator.MoveNext())
            {
                XmlLinkedNode nodo = (System.Xml.XmlLinkedNode)numerator.Current;
                if (nodo.Name == "Style")
                {
                    foreach (XmlNode elemento in nodo.ChildNodes)
                    {
                        if (!(elemento is XmlComment))
                        {
                            if (elemento.Name == "Fonts")
                            {
                                foreach (XmlNode fontElemento in elemento.ChildNodes)
                                {
                                    if (fontElemento.Name == titoloFont)
                                    {
                                        foreach (XmlNode valore in fontElemento.ChildNodes)
                                        {
                                            if (valore.Name == "Family")
                                                fontName = valore.InnerText;
                                            if (valore.Name == "Size")
                                                fontSize = Convert.ToInt32(valore.InnerText);
                                            if (valore.Name == "Bold")
                                                fontBold = Convert.ToBoolean(valore.InnerText);
                                            if (valore.Name == "Italic")
                                                fontItalic = Convert.ToBoolean(valore.InnerText);
                                            if (valore.Name == "Underline")
                                                fontUnderline = Convert.ToBoolean(valore.InnerText);
                                            if (valore.Name == "Color")
                                                colore = valore.InnerText;
                                        }
                                        break;
                                    }
                                }

                            }

                        }
                    }

                }

            }

            FontStyle stile = new FontStyle(fontName, fontSize, fontBold, fontItalic, fontUnderline, colore);

            return stile;
        }
    }
}
